var express = require('express');
var router = express.Router();
var sqlite3 = require('sqlite3').verbose();
var path = require('path');
/* GET users listing. */
router.get('/', function (req, res, next) {
    if (req.query.test_login == "1") {
        if (req.session.username == undefined || req.session.username == "")
            res.send('0');
    } else if (req.query.login_out == "1") { //退出登录
        req.session.username = "";
        res.send('1');
    } else {
        var db = new sqlite3.Database(path.join(__dirname, "../db/Delphi.s3db"));
        try {
            sqlStr1 = "SELECT s_expert_name from s_expert Where s_expert_id = ? and pwd = ?";
            sqlStr2 = "SELECT p_expert_name from p_expert Where p_expert_id= ? and pwd = ?";
            sqlStr3 = "SELECT visitor_name from visitor Where visitor_id = ? and pwd = ?";
            sqlStr4 = "SELECT admin_name from admin Where admin_id = ? and pwd = ?";
            db.all(sqlStr1, [req.query.username, req.query.password], function (err, rows) {
                if (rows.length == 0) {
                    db.all(sqlStr2, [req.query.username, req.query.password], function (err, rows) {
                        if (rows.length == 0) {
                            db.all(sqlStr3, [req.query.username, req.query.password], function (err, rows) {
                                if (rows.length == 0) {
                                    db.all(sqlStr4, [req.query.username, req.query.password], function (err, rows) {
                                        if (rows.length == 0) {
                                            res.send('0');
                                        } else {
                                            req.session.username = req.query.username;
                                            data = {
                                                name: rows[0]['admin_name'],
                                                tag: 4
                                            };
                                            res.send(data);
                                        }
                                    });
                                } else {
                                    req.session.username = req.query.username;
                                    data = {
                                        name: rows[0]['visitor_name'],
                                        tag: 3
                                    };
                                    res.send(data);
                                }
                            });
                        } else {
                            req.session.username = req.query.username;
                            data = {
                                name: rows[0]['p_expert_name'],
                                tag: 2
                            };
                            res.send(data);
                        }
                    });
                } else {
                    req.session.username = req.query.username;
                    data = {
                        name: rows[0]['s_expert_name'],
                        tag: 1
                    };
                    res.send(data);
                }
            });
        } catch (ex) {
            console.log(ex);
        }
    }
});

module.exports = router;